package com.ttyec.park.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ttyec.park.R;
import com.ttyec.park.SetUrlActivity;
import com.ttyec.park.Utils;
import com.ttyec.park.common.SharedPreferencesHelper;
import com.ttyec.park.homeActivity;
import com.ttyec.park.websocket.AppResponseDispatcher;
import com.zhangke.websocket.WebSocketService;
import com.zhangke.websocket.WebSocketSetting;

public class BaseSetFragment extends Fragment implements View.OnClickListener {
    private EditText et_url;  // 请输入网址，不要加http://
    private EditText et_token;  // 请输入Token

    SharedPreferencesHelper helper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_baseset, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        et_url = view.findViewById(R.id.et_url);
        et_token = view.findViewById(R.id.et_token);
        TextView tv_save = view.findViewById(R.id.tv_save);
        tv_save.setOnClickListener(this);
        initData();
    }

    private void initData() {
        helper = new SharedPreferencesHelper(getActivity());
        String host = (String) helper.getSharedPreference(Utils.SP_WS_HOST, "");
        String token = (String) helper.getSharedPreference(Utils.SP_WS_TOKEN, "");
        if (!TextUtils.isEmpty(host)) {
            et_url.setText(host);
        }
        if (!TextUtils.isEmpty(token)) {
            et_token.setText(token);
        }
    }

    @Override
    public void onClick(View v) {
        String url = et_url.getText().toString().trim();
        String token = et_token.getText().toString().trim();
        if (TextUtils.isEmpty(url)) {
            Toast.makeText(getActivity(), "请输入ip", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(token)) {
            Toast.makeText(getActivity(), "请输入端口号", Toast.LENGTH_SHORT).show();
            return;
        }
        // ws://test.ttyec.com:80/websocket/socketServer?token=2b716d740f644f289002e88fbd1303ac
        StringBuilder resultUrl = new StringBuilder("ws://")
                .append(url).append("/websocket/socketServer?token=")
                .append(token);
        //配置 WebSocket，必须在 WebSocket 服务启动前设置
        WebSocketSetting.setConnectUrl(resultUrl.toString());//必选
        WebSocketSetting.setResponseProcessDelivery(new AppResponseDispatcher());
        WebSocketSetting.setReconnectWithNetworkChanged(true);
        //启动 WebSocket 服务
        if (getActivity() != null) {
            SharedPreferencesHelper helper = new SharedPreferencesHelper(getActivity());
            helper.put(Utils.SP_WS_URL, resultUrl);
            helper.put(Utils.SP_WS_HOST, url);
            helper.put(Utils.SP_WS_TOKEN, token);
            getActivity().startService(new Intent(getActivity(), WebSocketService.class));
            if (getActivity() instanceof SetUrlActivity) {
                getActivity().startActivity(new Intent(getActivity(),homeActivity.class));
                getActivity().finish();
            }else if(getActivity() instanceof homeActivity){
                getActivity().startActivity(new Intent(getActivity(),homeActivity.class));
                getActivity().finish();
            }
        }
    }
}
